<?php
include('connect.php');
echo "<h1>Register</h1>";
$submit=$_POST['submit'];
//form data
$firstname = strip_tags($_POST['firstname']);
$minitial= strip_tags($_POST['minitial']);
$lastname= strip_tags($_POST['lastname']);
$email = strip_tags($_POST['email']);
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date=date("y-m-d");
if($submit)
{

//check for existance
if($firstname&&$lastname&&$email&&$password&&$repeatpassword)
{
  if($password==$repeatpassword)
  {
    if(strlen($firstname)>25||strlen($lastname)>25){
      echo "Max limit for firstname/lastname are 25 characters";
    }
    if(strlen($password)>25||strlen($password)<6){
      echo "Password must be between 6 and 25 characters";
      
    }
    else{
      //register the user
      $password=md5($password);
      $repeatpassword=md5($repeatpassword);
      $connect=mysql_connect(DB_HOST,DB_USER,DB_PASS);
      mysql_select_db("N02233598?db");
      $emailcheck=mysql_query("SELECT email FROM users WHERE email='$email'");
      $count=mysql_num_rows($emailcheck);
      if($count!=0)
      {
	   die("Email already registerd!");
	}
      $queryreg=mysql_query("
	INSERT INTO users VALUES('','$firstname','minitial','$lastname','$email','$password','$date')
	");
echo "You have been register! <a href='index.php'>Return to login page</a>";
    }
   }
   else
     echo "Passwords don't match!";
}
else
   echo "Please complete all required section";

}
?>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css" />

<form action='register.php' method='Post'>
 <table>
	<tr>
	    <td>
	    Your first name:	
	    </td>
	    <td>
	    <input type='text' name='firstname' value=<?php echo $firstname; ?>>
	    </td>
	</tr>
	<tr>
	    <td>
	    Your middle initial(Optional):	
	    </td>
	    <td>
	    <input type='text' name='minitial' value=<?php echo $minitial; ?>>
	    </td>
	</tr>
	<tr>
	    <td>
	    Your last name:	
	    </td>
	    <td>
	    <input type='text' name='lastname' value=<?php echo $lastname; ?>>
	    </td>
	</tr>
	<tr>
	    <td>
	    Enter your email (this will be your username):	
	    </td>
	    <td>
	    <input type='text' name='email' value=<?php echo $email; ?>>
	    </td>
	</tr>
	<tr>
	    <td>
	    Chose a password:	
	    </td>
	    <td>
	    <input type='password' name='password'>
	    </td>
	</tr>
	<tr>
	    <td>
	    Retype Password:	
	    </td>
	    <td>
	    <input type='password' name='repeatpassword'>
	    </td>
	</tr>
 </table>
<p>
  <input type='submit' name='submit' value='Register'>
</form>
</html>
